<template>
  <div class="login-container ub main-center cross-center">
    <el-form
      @submit.native.prevent="submit"
      size="medium"
      :rules="rules"
      ref="loginForm"
      :model="loginForm"
      label-width="120px"
      class="login-form"
    >
      <el-form-item label>
        <div class="login-title ub main-center cross-center">系统登录</div>
      </el-form-item>
      <el-form-item prop="userName" label>
        <el-input type="text" v-model="loginForm.userName" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password" label>
        <el-input
          :type="passwordType"
          v-model="loginForm.password"
          placeholder="请输入密码"
          :key="passwordType"
        ></el-input>
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          <el-button type icon="el-icon-view"></el-button>
        </span>
      </el-form-item>
      <el-form-item prop="code" label>
        <el-row :gutter="10">
          <el-col :span="16">
            <el-input type="text" v-model="loginForm.code" placeholder="请输入验证码"></el-input>
          </el-col>
          <el-col :span="8">
            <img @click="getImage" :src="imgSrc" class="codeImg" />
            <!-- <el-input type="text" placeholder="点击刷新" readonly></el-input> -->
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-button class="my-button" @click="submit" type="primary">登录</el-button>
          </el-col>
          <el-col :span="12">
            <el-button class="my-button" type="primary" @click="reset">重置</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </div>
</template>
  
  <script>
export default {
  data() {
    return {
      passwordType: "password",
      imgSrc: "http://localhost:8083/api/user/image",
      // imgSrc: "http://120.25.157.203:8083/api/user/image",
      // imgSrc:"https://dummyimage.com/200%20x%20100/f2d079/fff.png&text=huynbrb",
      loginForm: {
        userName: "",
        password: "",
        code: "",
      },
      rules: {
        userName: [
          {
            required: true,
            trigger: "change",
            message: "请输入用户名",
          },
        ],
        password: [
          {
            required: true,
            trigger: "change",
            message: "请输入密码",
          },
        ],
        code: [
          {
            required: true,
            trigger: "change",
            message: "请输入验证码",
          },
        ],
      },
    };
  },
  methods: {
    reset() {
      this.loginForm.userName = "";
      this.loginForm.password = "";
      this.loginForm.code = "";
    },
    async submit() {
      const _this = this;
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          // console.log(valid)
          //菜单验证成功后，我们要进行用户认证，需要将用户名和密码以及验证码传输给服务器进行认证。
          console.log("这是获取的密码" + _this.loginForm.password);
          let datafor = new FormData();
          datafor.append("username", _this.loginForm.userName);
          datafor.append("password", _this.loginForm.password);
          datafor.append("code", _this.loginForm.code);
          let { data: res } = await _this.$http.post(
            "/api/user/login",
            datafor
          );
          console.log(res);
          if (res.code != 200) {
            _this.$message.error(res.msg);
            return;
          }
          _this.$message.success(res.msg);
          //模拟从后台数据库中获得菜单数据和路由数据
          //菜单数据
          let token = res.data.token;
          let menuList = res.data.menuList;

          //路由数据
          let routerList = res.data.routerList;

          let auths = res.data.authList;
          //在sessionStore里面保存菜单数据和路由数据
          //保存菜单数据
          sessionStorage.setItem("token", token);
          sessionStorage.setItem("auths", JSON.stringify(auths));
          sessionStorage.setItem("menuList", JSON.stringify(menuList));
          //保存路由数据
          sessionStorage.setItem("routerList", JSON.stringify(routerList));
          //动态生成路由
          this.$store.commit("getMenuList", this.$router);
          //跳转到home页面
          this.$router.push("home");
        }
      });
    },
    //获取验证码
    getImage() {
      let res =
        // "http://120.25.157.203:8083/api/user/image?t=" + new Date().getTime();
        "http://localhost:8083/api/user/image?t="+new Date().getTime();
      this.imgSrc = res;
    },
    //显示密码
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
  },
  mounted: () => {
    this.getImage();
  },
};
</script>
  

<style  scoped>
/* .show-pwd {
  position: absolute;
  right: 10px;
  top: 7px;
  font-size: 16px;
  color: $dark_gray;
  cursor: pointer;
  user-select: none;
} */
.login-container {
  height: 100%;
}
.login-title {
  font-size: 24px;
  font-weight: 600;
}
.login-form {
  height: 300px;
  width: 350px;
  border-radius: 10px;
  box-shadow: 0 0 25px #cac6c6;
  padding: 20px 35px;
}
.my-button {
  width: 100%;
}
.login-container /deep/ .el-form-item__content {
  margin-left: 0 !important;
}
.codeImg {
  width: 100%;
  cursor: pointer;
}
</style>